﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Common;
using ESOL.Common.Entity;

namespace ESOL.WinUI.MasterData
{
    public partial class frmMaintainCommonItem : BaseForm.frmBaseDialogyMaintainForm
    {
        #region Variable
        public MstCommonItemEntity SelectedCommonItem { get; set; }
        #endregion

        #region Constructor
        public frmMaintainCommonItem()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByEnum(cmbIsDelete, typeof(DeleteFlag), typeof(string), false);
            UICommon.UICommonFunctions.BindGridComboboxByEnum(colIsDeleted, typeof(DeleteFlag), typeof(int), true);

            DataTable dt = MstCommonItemBs.GetAllItemType();
            DataRow dr = dt.NewRow();
            dr["ITEM_TYPE"] = "";
            dt.Rows.InsertAt(dr, 0);
            cmbItemType.DataSource = dt;
            cmbItemType.DisplayMember = "ITEM_TYPE";
            cmbItemType.ValueMember = "ITEM_TYPE";
        }

        private void BindDataGrid(string itemType)
        {
            if (string.IsNullOrEmpty(itemType))
            {
                gvMain.DataSource = null;
            }
            DataTable dt = MstCommonItemBs.GetMstCommonItemDtByType(itemType);
            gvMain.DataSource = dt;
        }

        private void SetFields()
        {
            if (SelectedCommonItem == null)
            {
                return;
            }
            txtItemCode.Text = SelectedCommonItem.ItemCode;
            txtItemName.Text = SelectedCommonItem.ItemName;
            txtItemValue.Text = SelectedCommonItem.ItemValue;
            txtRemark.Text = SelectedCommonItem.Remark;
            cmbIsDelete.SetSelectedValue(SelectedCommonItem.IsDeleted);
            cmbItemType.SetSelectedValue(SelectedCommonItem.ItemType);
        }

        private void SetEntity()
        {
            if (SelectedCommonItem == null)
            {
                SelectedCommonItem = new MstCommonItemEntity();
            }
            SelectedCommonItem.ItemCode = txtItemCode.Text.ToUpper();
            SelectedCommonItem.ItemName = txtItemName.Text;
            SelectedCommonItem.ItemValue = txtItemValue.Text;
            SelectedCommonItem.Remark = txtRemark.Text;
            if (cmbIsDelete.SelectedValue != null && cmbIsDelete.SelectedValue.ToString().Trim() != "")
            {
                SelectedCommonItem.IsDeleted = CommonFunctions.ToInt32(cmbIsDelete.SelectedValue);
            }
            if (string.IsNullOrEmpty(cmbItemType.Text) == false)
            {
                SelectedCommonItem.ItemType = cmbItemType.Text.ToUpper();
            }
        }

        private bool CheckData()
        {
            if (string.IsNullOrEmpty(txtItemCode.Text))
            {
                MessageBox.Show("项目编码不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(cmbItemType.Text))
            {
                MessageBox.Show("项目类型不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtItemName.Text))
            {
                MessageBox.Show("项目名称不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }

        private bool Save()
        {
            try
            {
                if (CheckData() == false)
                {
                    return false;
                }
                SetEntity();
                if (ThisActionType == ActionType.New)
                {
                    bool isSuccessful = MstCommonItemBs.InsertMstCommonItem(SelectedCommonItem);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("新增失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.Modify)
                {
                    bool isSuccessful = MstCommonItemBs.UpdateMstCommonItem(SelectedCommonItem);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("修改失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        #endregion

        #region Event
        private void frmMaintainCommonItem_Load(object sender, EventArgs e)
        {
            BindComboBox();
            SetFields();
            if (ThisActionType == ActionType.New)
            {
                txtItemCode.Properties.ReadOnly = false;
            }
            else
            {
                txtItemCode.Properties.ReadOnly = true;
            }
            if (SelectedCommonItem != null)
            {
                BindDataGrid(SelectedCommonItem.ItemType);
            }
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            if (ThisActionType == ActionType.View)
            {
                btnSaveNew.Visible = false;
            }
        }

        protected override void Click_Save()
        {
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void btnSaveNew_Click(object sender, EventArgs e)
        {
            if (Save())
            {
                ThisActionType = ActionType.New;
                txtItemCode.Properties.ReadOnly = false;
                SelectedCommonItem = new MstCommonItemEntity();
                SelectedCommonItem.ItemType = cmbItemType.Text;
                SetFields();
                BindDataGrid(cmbItemType.Text);
            }
        }

        private void txtItemCode_Leave(object sender, EventArgs e)
        {
            txtItemCode.Text = txtItemCode.Text.ToUpper();
        }

        private void cmbItemType_Leave(object sender, EventArgs e)
        {
            cmbItemType.Text = cmbItemType.Text.ToUpper();
            BindDataGrid(cmbItemType.Text);
        }
        #endregion


    }
}
